<template>
  <div class="personal-center-container">
    <div class="container-title">
      <vab-query-form>
      <vab-query-form-top-panel>
        <el-form-item>
          <el-input v-model.trim="queryForm.title" class="input_w" clearable placeholder="请输入标题" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryForm.category_id" class="input_w" clearable placeholder="请选择分类"
            style="margin-left: 10px">
            <el-option v-for="item in categoryList" :key="item.id" :label="item.category_name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button :icon="Search" native-type="submit" type="primary" @click="queryData">
            查询
          </el-button>
          <el-button :icon="Plus" type="primary" @click="handleAdd">
            创建
          </el-button>
        </el-form-item>
      </vab-query-form-top-panel>
    </vab-query-form>

    <vab-card class="h-user" :skeleton="true" v-loading="listLoading">
      <el-row :gutter="20">
        <el-col v-for="item in list" :key="item.corp_id" :lg="8" :md="8" :sm="8" :xl="6" :xs="12">
          <vab-card shadow="hover">
            <el-row justify="space-between">
              <div class="row-content">
                <div class="department-name">
                  标题：{{ item.title }}
                </div>
                <div class="department-name">
                  分类：{{ categoryFilter(item.category_id) }}
                </div>
              </div>
            </el-row>
            <el-row justify="end">
              <el-button class="but-padding" type="primary" @click="handleEdit(item)">
                编辑
              </el-button>
              <el-button class="but-padding" type="danger" @click="handleDelete(item)">
                删除
              </el-button>
            </el-row>
          </vab-card>
        </el-col>
      </el-row>
      <el-row v-if="list.length == 0" justify="center">
        <el-empty description="暂无内容" />
      </el-row>
    </vab-card>
    <el-pagination style="margin-top: 0;margin-bottom: 20px;"background :current-page="queryForm.page" :layout="layout" :page-size="queryForm.page_size"
      :total="total" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
    </div>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { getCategoryAll } from '@/api/enterpriseWeChat/picTitleCategory'
import { getTitleList, doDelete } from '@/api/enterpriseWeChat/titleList'
import VabDraggable from 'vuedraggable'
import { Delete, Plus, Search, Setting } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'TitleList',
  components: {
    Edit: defineAsyncComponent(() => import('./components/TableEdit')),
    VabDraggable,
  },
  setup() {
    const $baseConfirm = inject('$baseConfirm')
    const $baseMessage = inject('$baseMessage')
    const $baseTableHeight = inject('$baseTableHeight')

    const state = reactive({
      editRef: null,
      list: [],
      categoryList: [],
      listLoading: true,
      height: $baseTableHeight(1),
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      queryForm: {
        page: 1,
        page_size: 20,
        title: '',
        category_id: '',
      },
    })
    const categoryData = async () => {
      const { data } = await getCategoryAll()
      state.categoryList = data
      console.log(data)
    }
    const fetchData = async () => {
      state.listLoading = true
      const {
        data: { list, total },
      } = await getTitleList(state.queryForm)
      console.log(list)
      state.list = list
      state.total = total
      state.listLoading = false
    }
    const handleSizeChange = (val) => {
      state.queryForm.page_size = val
      const pageMax = Math.ceil(state.total / val)
      if (state.queryForm.page <= pageMax) {
        fetchData()
      }
    }
    const handleCurrentChange = (val) => {
      state.queryForm.page = val
      fetchData()
    }
    const queryData = () => {
      state.queryForm.page = 1
      fetchData()
    }
    const categoryFilter = (id) => {
      console.log(id)
      let str = ''
        state.categoryList.map((item) => {
          if (item.id == id) {
            str = item.category_name
          }
        })
        return str
    }
    const handleAdd = () => {
      state['editRef'].showEdit(state.categoryList)
    }
    const handleEdit = (row) => {
      state['editRef'].showEdit(state.categoryList, row)
    }
    const handleDelete = (row) => {
      if (row.id) {
        $baseConfirm('你确定要删除当前项吗', null, async () => {
          const { message } = await doDelete({ id: row.id })
          $baseMessage(message, 'success', 'vab-hey-message-success')
          await fetchData()
        })
      }
    }
    onMounted(() => {
      categoryData()
      fetchData()
    })

    return {
      ...toRefs(state),
      handleSizeChange,
      handleCurrentChange,
      categoryFilter,
      queryData,
      fetchData,
      categoryData,
      handleAdd,
      handleEdit,
      handleDelete,
      Plus,
      Delete,
      Search,
      Setting,
    }
  },
})
</script>
<style lang="scss" scoped>
.container-title {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
  }
.h-user {
  flex-grow: 1;
  :deep() {
  .el-card__body {
    padding: 10px 15px !important;
  }
}
}

.but-padding {
  height: 25px;
  font-size: 12px;
  padding: 0 6px;
}

.row-content {
  margin-bottom: 20px;

  .department-name {
    font-size: 14px;
    color: #7d7d7d;
    margin-top: 8px;
  }
}
$base: '.personal-center';
  #{$base}-container {
    padding: 0 !important;
    background: $base-color-background !important;
    display: flex;
    flex-direction: column;
  }
</style>
